package www.ym.com.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import www.ym.com.common.R;
import www.ym.com.entity.MenuRole;
import www.ym.com.service.MenuRoleService;

import java.io.Serializable;
import java.util.List;

@RestController
@RequestMapping("/roleMenu")
public class MenuRoleController {

    @Autowired
    private MenuRoleService menuRoleService;

    /**
     * 权限配置
     * @param menuRole
     * @return
     */
    @PostMapping("/add")
    public R<String> add(@RequestBody List<MenuRole> menuRole) {
        //删除之前的权限菜单配置
        Long roleId = menuRole.get(0).getRoleId();
        LambdaQueryWrapper<MenuRole> lambda = new LambdaQueryWrapper<>();
        lambda.eq(MenuRole::getRoleId, roleId);
        if(lambda!=null){
            menuRoleService.remove(lambda);
        }
        menuRoleService.saveBatch(menuRole);
        return R.success("", "配置成功");
    }

    /**
     * 回显权限管理
     * @param id
     * @return
     */
    @GetMapping("/getPermission")
    public  R<List<MenuRole>> echoPermission(Long id){
        LambdaQueryWrapper<MenuRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(MenuRole::getRoleId,id);
        List<MenuRole> list = menuRoleService.list(wrapper);
        return  R.success(list,"");
    }

}
